<#
规则重构计划生成（原生 PowerShell）
用途：扫描 rules/*.md 标题，生成 .tmp/rules_refactor_plan.md 初稿。
用法：
  pwsh -File scripts/windows/refactor_rules.ps1
#>
param()

$WindowsDir = Split-Path -Parent $PSCommandPath
$ScriptsDir = Split-Path -Parent $WindowsDir
$RepoDir = Split-Path -Parent $ScriptsDir

$rulesDir = Join-Path $RepoDir 'rules'
if (!(Test-Path $rulesDir)) { $rulesDir = Join-Path $RepoDir 'rules' }
$outDir = Join-Path $RepoDir '.tmp'
if (!(Test-Path $outDir)) { $outDir = Join-Path $RepoDir '.tmp' }
New-Item -ItemType Directory -Force -Path $outDir | Out-Null
$out = Join-Path $outDir 'rules_refactor_plan.md'

$lines = @('# 规则重构计划（自动生成）','')
Get-ChildItem -Path $rulesDir -File -Include *.md | ForEach-Object {
  $txt = Get-Content -Raw $_.FullName
  $title = ($txt -split "`n") | Where-Object { $_.Trim().Length -gt 0 } | Select-Object -First 1
  $rel = $_.FullName.Substring($RepoDir.Length).TrimStart('\','/')
  $lines += "- [$title]($rel)"
}

Set-Content -Path $out -Value ($lines -join "`n")
Write-Host "已生成：$out"
exit 0
